/*
 * @Author: ZWS
 * @Date: 2022-02-12 17:30:49
 * @LastEditTime: 2022-02-14 17:19:50
 * @Description: 状态管理器
 */
import { createStore } from 'vuex'
import VuexPersist from 'vuex-persist'
// vuex 数据持久化插件
const vuexPersist: VuexPersist<object> = new VuexPersist({
  storage: window.sessionStorage
})

// 动态加载vuex模块
const modulesFile: Record<string, any> = import.meta.globEager('./modules/*.ts')
const modules: any = {}
Object.keys(modulesFile).forEach(path => {
  const fileNameList: RegExpMatchArray | null = path.match(/.\/modules\/(\S*).ts/)
  if (fileNameList === null) {
    return
  }
  const modulesNmae: string = fileNameList[1]
  modules[modulesNmae] = modulesFile[path].default
})

const store = createStore({
  state: {},
  mutations: {},
  getters: {},
  actions: {},
  plugins: [vuexPersist.plugin],
  modules
})

export default store
